<?php
/**
 * StaticPageController is the default controller for this application
 */
class StaticPageController extends ControllerBase {
	/**
	 *
	 * PackageOptionDataService Object
	 * @var PackageOptionsDataService
	 */
	private $packageOptionDataService = null;
	
	public function __construct() {
		parent::__construct ();
		
		// Set page name
		$this->setPageName ( 'page' );
		
		// Init all DataService
		$this->packageOptionDataService = new PackageOptionsDataService ();
	}
	
	/**
	 * The "index" action is the default action for all controllers -- the
	 * landing page of the site.
	 *
	 * @return void
	 */
	public function indexAction() {
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/StaticPage/index.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}
	
	public function editAction() {
		// get page
		$page = $this->getPost ( "page", "" );
		if (empty ( $page )) {
			$page = $this->getQuery ( "page", "" );
		}
		
		// list page
		$listPages = array ("HomeDescription", "Contact", "TermAndCondition", "PriceList", "PriceOption1", "PriceOption2", "PriceOption3", "PriceOption4" );
		
		if (! in_array ( $page, $listPages )) {
			// Redirect Page to
			$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Admin/';
			CommonUtils::redirect ( $url );
			exit ();
		}
		
		$actionForm = $this->getPost ( "actionForm", "" );
		$description = $this->getPost ( "description", "" );
		$headTitle = $this->getPost ( "headTitle", "" );
		$metaDescription = $this->getPost ( "metaDescription", "" );
		$metaKeywords = $this->getPost ( "metaKeywords", "" );
		
		$metaTitleName = "";
		$metaKeywordsName = "";
		$metaDescriptionName = "";
		
		switch ($page) {
			case "HomeDescription" :
				$fileName = APPLICATION_PATH . '/Views/Templates/Browse/Index/homepage_description.tpl';
				$metaTitleName = SETTINGS_HOMEPAGE_TITLE;
				$metaKeywordsName = SETTINGS_HOMEPAGE_META_KEYWORDS;
				$metaDescriptionName = SETTINGS_HOMEPAGE_META_DESCRIPTION;
				break;
			case "Contact" :
				$fileName = APPLICATION_PATH . '/Views/Templates/Browse/Index/contact.tpl';
				$metaTitleName = SETTINGS_CONTACT_TITLE;
				$metaKeywordsName = SETTINGS_CONTACT_META_KEYWORDS;
				$metaDescriptionName = SETTINGS_CONTACT_META_DESCRIPTION;
				break;
			case "TermAndCondition" :
				$fileName = APPLICATION_PATH . '/Views/Templates/Browse/TermAndCondition/term_condition.tpl';
				//$metaTitleName = SETTINGS_CONTACT_TITLE;
				//$metaKeywordsName = SETTINGS_CONTACT_META_KEYWORDS;
				//$metaDescriptionName = SETTINGS_CONTACT_META_DESCRIPTION;
				break;
			case "PriceList" :
				
				$fileName = APPLICATION_PATH . '/Views/Templates/Browse/Price/price_overview.tpl';
				$metaTitleName = SETTINGS_PRICE_LIST_TITLE;
				$metaKeywordsName = SETTINGS_PRICE_LIST_META_KEYWORDS;
				$metaDescriptionName = SETTINGS_PRICE_LIST_META_DESCRIPTION;
				break;
			case "PriceOption1" :
				$fileName = APPLICATION_PATH . '/Views/Templates/Browse/Price/price_option_1.tpl';
				$metaTitleName = SETTINGS_PRICE_OPTION_1_TITLE;
				$metaKeywordsName = SETTINGS_PRICE_OPTION_1_META_KEYWORDS;
				$metaDescriptionName = SETTINGS_PRICE_OPTION_1_META_DESCRIPTION;
				break;
			case "PriceOption2" :
				$fileName = APPLICATION_PATH . '/Views/Templates/Browse/Price/price_option_2.tpl';
				$metaTitleName = SETTINGS_PRICE_OPTION_2_TITLE;
				$metaKeywordsName = SETTINGS_PRICE_OPTION_2_META_KEYWORDS;
				$metaDescriptionName = SETTINGS_PRICE_OPTION_2_META_DESCRIPTION;
				break;
			case "PriceOption3" :
				$fileName = APPLICATION_PATH . '/Views/Templates/Browse/Price/price_option_3.tpl';
				$metaTitleName = SETTINGS_PRICE_OPTION_3_TITLE;
				$metaKeywordsName = SETTINGS_PRICE_OPTION_3_META_KEYWORDS;
				$metaDescriptionName = SETTINGS_PRICE_OPTION_3_META_DESCRIPTION;
				break;
			case "PriceOption4" :
				$fileName = APPLICATION_PATH . '/Views/Templates/Browse/Price/price_option_4.tpl';
				$metaTitleName = SETTINGS_PRICE_OPTION_4_TITLE;
				$metaKeywordsName = SETTINGS_PRICE_OPTION_4_META_KEYWORDS;
				$metaDescriptionName = SETTINGS_PRICE_OPTION_4_META_DESCRIPTION;
				break;
		}
		$settingsDataService = new SettingsDataService ();
		$isUpdateSuccessfull = 0;
		if (! empty ( $actionForm ) && ! empty ( $description ) && $actionForm == "submit") {
			// magic quote
			if (get_magic_quotes_gpc ()) {
				$description = stripslashes ( $description );
			}
			
			$description = htmlspecialchars_decode ( $description, ENT_QUOTES );
			
			// write file successfully
			try {
				file_put_contents ( $fileName, $description );
				$content = $description;
				$isUpdateSuccessfull = 1;
				if ($page != "TermAndCondition") {
					$settingsDataService->setValue ( $metaTitleName, $headTitle );
					$settingsDataService->setValue ( $metaKeywordsName, $metaKeywords );
					$settingsDataService->setValue ( $metaDescriptionName, $metaDescription );
				}
			} catch ( Exception $e ) {
				$content = file_get_contents ( $fileName );
			}
		} else {
			$content = file_get_contents ( $fileName );
			if ($page != "TermAndCondition") {
				$headTitle = $settingsDataService->getValue ( $metaTitleName );
				$metaKeywords = $settingsDataService->getValue ( $metaKeywordsName );
				$metaDescription = $settingsDataService->getValue ( $metaDescriptionName );
			}
		}
		
		$this->smarty->assign ( 'page', $page );
		if ($page != "TermAndCondition") {
			$this->smarty->assign ( 'headTitle', $headTitle );
			$this->smarty->assign ( 'metaKeywords', $metaKeywords );
			$this->smarty->assign ( 'metaDescription', $metaDescription );
		}
		$this->smarty->assign ( 'isUpdateSuccessfull', $isUpdateSuccessfull );
		
		$this->smarty->assign ( 'description', $content );
		
		if ($page == "PriceList") {
			$arrElement = array ();
			$arrElement ['status'] = PackageOptionsEntity::ACTIVE;
			$packages = $this->packageOptionDataService->getItems ( $arrElement, " name " );
			
			$this->smarty->assign ( 'option1', $packages [0] );
			$this->smarty->assign ( 'option2', $packages [1] );
			$this->smarty->assign ( 'option3', $packages [2] );
			$this->smarty->assign ( 'option4', $packages [3] );
			
			//This case need to be processed different
			$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/StaticPage/edit_pricelist.tpl' );
		} else {
			$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/StaticPage/edit.tpl' );
		}
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}
}
